/*
 __   _____  _     ___     _   _ _   _ ___ _____ 
 \ \ / / _ \| |   / _ \   | \ | | \ | |_ _| ____|
  \ V / | | | |  | | | |  |  \| |  \| || ||  _|  
   | || |_| | |__| |_| |  | |\  | |\  || || |___ 
   |_| \___/|_____\___/___|_| \_|_| \_|___|_____|
                     |_____|                     
  (C)2024 YOLO NNIE

 * Description:
        YOLO nnie detection : Yolo nnie project trace helpers

 * License: halokitor @ xidian university
 *
 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef SVP_NNIE_APPLICATION_H
#define SVP_NNIE_APPLICATION_H

#include <pthread.h>
#include "sample_comm.h" 
#include "hi_comm_svp.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

/* -------------------------------------------------------------------------- */
/* --- PRIVATE MACROS ------------------------------------------------------- */
#define SVP_NNIE_YOLOV5_RESULT_OUTPUT_NUM   3
#define SAMPLE_SVP_NNIE_QUANT_BASE          4096.0f
#define SAMPLE_SVP_NNIE_MAX(a, b)           (((a) > (b)) ? (a) : (b))
#define SAMPLE_SVP_NNIE_MIN(a, b)           (((a) < (b)) ? (a) : (b))
#define SAMPLE_SVP_NNIE_SIGMOID(x)          (HI_FLOAT)((1.0f) / (1.0f + exp((HI_DOUBLE)(-x))))

#define HI_PER_BASE                100
#define HI_OVEN_BASE               2        // Even base

#define SAMPLE_IVE_KCF_MAX_BBOX    64
/* -------------------------------------------------------------------------- */
/* --- PRIVATE TYPES -------------------------------------------------------- */

/* Yolov5 lite params setting */
#define YOLO_MODEL_WIDTH           320
#define YOLO_MODEL_HEIGHT          320

#define YOLO_FRAME_WIDTH           1920
#define YOLO_FRAME_HEIGHT          1080

#define CONF_THRESH                0.3
#define NMS_THRESH                 0.6

#define OFFSETIDX                  2

// #define pcYoloModelName            "./data/model/yolov5_lite_416yuv_rgb_inst_inst.wk"
#define pcYoloModelName            "./data/model/yolov5_lite_self320_high_yuv_rgb_inst.wk"
// #define pcYoloModelName            "./data/model/best_320_high_yuv_rgb_inst.wk"

typedef struct hiANCHOR_W_H
{
	HI_FLOAT anchor_w;
	HI_FLOAT anchor_h;
} ANCHOR_W_H;

typedef enum hiSAMPLE_IVE_KCF_PROC_STATUS_E {
    KCF_PROC_START  = 0x00,
    KCF_PROC_PROC   = 0x01,
    KCF_PROC_END    = 0x02,
    KCF_PROC_BUTT
} SAMPLE_IVE_KCF_PROC_STATUS_E;

typedef enum hiSAMPLE_IVE_LBPH_MATCH_PROC_STATUS_E {
    LBPH_PROC_START     = 0x00,
    LBPH_PROC_INITED    = 0x01,
    LBPH_PROC_END       = 0x02,
    LBPH_PROC_BUTT
} SAMPLE_IVE_LBPH_MATCH_PROC_STATUS_E;

/* Definition of rectangular box coordinates */
typedef struct s_sRectBox {
    HI_FLOAT xmin;
    HI_FLOAT xmax;
    HI_FLOAT ymin;
    HI_FLOAT ymax;
} RectBox;

typedef struct hiSAMPLE_IVE_KCF_S {
    SAMPLE_IVE_KCF_PROC_STATUS_E enKcfProcStat;
    HI_BOOL bUpdateDetect;
    RectBox trackRoi;
    RectBox gestureRoi;
    pthread_mutex_t ModifyMutex;
} SAMPLE_IVE_KCF_S;

typedef enum hiSVP_NNIE_SORT_MODE_S {
    SORT_SCORE_MODE = 0x00,
    SORT_CENTER_MODE = 0x01,
} SVP_NNIE_SORT_MODE_S;

typedef struct hiSVP_NNIE_YOLOV5_BBOX_S {
    RectBox bBox;
    HI_U8 u8ClassIdx;
    HI_FLOAT f32ClsScore;
    struct hiSVP_NNIE_YOLOV5_BBOX_S* next;
} SVP_NNIE_YOLOV5_BBOX_S;

void SVP_NNIE_Yolov5_HandleSig( void );

void SVP_NNIE_Yolov5_Lite( void );

HI_S32 PauseDoUnloadModel( HI_VOID );

HI_S32 Load_Yolov5_Lite_Model( uintptr_t* model, const HI_CHAR *pcModelName );

HI_S32 Yolov5DetectResnetClassifyCal(uintptr_t model, VIDEO_FRAME_INFO_S *srcFrm, VIDEO_FRAME_INFO_S *dstFrm, SAMPLE_IVE_KCF_S *pstIveKcfInfo);

HI_VOID ParseUartMsgProcess(HI_UCHAR *msg, SAMPLE_IVE_KCF_S *pstIveKcfInfo);

HI_S32 CaculateDiffCenter(RectBox bBox, HI_FLOAT cx);

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */

#endif /* SAMPLE_SVP_MAIN_H */
